//
//  UIImageView+WDExtention.m
//  MyDemos
//
//  Created by iMac on 2018/3/14.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "UIImageView+WDExtention.h"
#import <objc/runtime.h>


const char *tapImageViewCallbackKey = "tapImageViewCallbackKey";
@implementation UIImageView (WDExtention)


/// set image name nomal
- (UIImageView *(^)(NSString *))sImage {
    __weak typeof(self)weekSelf = self;
    return ^UIImageView *(NSString * imageName) {
        [weekSelf setImage:[UIImage imageNamed:imageName]];
        return weekSelf;
    };
}

/// set image name highlighted
- (UIImageView *(^)(NSString *))sImageHighlighted {
    __weak typeof(self)weekSelf = self;
    return ^UIImageView *(NSString *imageName) {
        [weekSelf setHighlightedImage:[UIImage imageNamed:imageName]];
        return weekSelf;
    };
}

/// set highlighted
- (UIImageView *(^)(BOOL))sHightlighted {
    __weak typeof(self)weekSelf = self;
    return ^UIImageView *(BOOL isHighlighted) {
        [weekSelf setHighlighted:isHighlighted];
        return weekSelf;
    };
}

/// set frame
- (UIImageView *(^)(CGRect))sFrame {
    __weak typeof(self)weekSelf = self;
    return ^UIImageView *(CGRect frame) {
        [weekSelf setFrame:frame];
        return weekSelf;
    };
}


/// tap gesture
- (void)tap:(void(^)(UIImageView *view))callback {
    self.tapCallback = callback;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init];
    [gesture addTarget:self action:@selector(imageViewClick:)];
    [self addGestureRecognizer:gesture];
}
- (void)imageViewClick:(UIImageView *)sender {
    if (self.tapCallback) {
        self.tapCallback(sender);
    }
}
- (void)setTapCallback:(void (^)(UIImageView *))tapCallback {
    objc_setAssociatedObject(self, tapImageViewCallbackKey, tapCallback, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(UIImageView *))tapCallback {
    return objc_getAssociatedObject(self, tapImageViewCallbackKey);
}

@end
